<?php
/**
 * @version		1.0: classProducent.php marum56
 * @package		Joomla
 */

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

jimport( 'joomla.html.parameter');

/**
 * Producent class.  
 *
 * @package 	Joomla
 * @subpackage	Producent
 * @since	1.0
 */
class msProducent extends JObject
{
	/**
	 * Unique id
	 * @var int
	 */
	var $id				= null;
	
	/**
	 * Error message
	 * @var string
	 */
	var $_errorMsg			= null;


	/**
	* Constructor
	*
	* @access 	protected
	*/
	function __construct($identifier = 0)
	{
		// Load the producer if it exists
		if (!empty($identifier)) {
			$this->load($identifier);
		}
		else {
			//initialise
			$this->id        = 0;
			$this->nrp       = 0;
		}
	}

	/**
	 * Returns a reference to the global Producent object, only creating it if it
	 * doesn't already exist.
	 *
	 * This method must be invoked as:
	 * 		<pre>  $producent =& msProducent::getInstance($id); </pre>
	 *
	 * @access 	public
	 * @param 	int 	$id 	Producent do zaladowania.
	 * @return 	msProducent  	The Producent object.
	 * @since 	1.0
	 */
	function &getInstance($id = 0)
	{
		static $instances = array ();
		

		if (empty($instances[$id])) {
			$producent = new msProducent($id);
			$instances[$id] = $producent;
		}

		return $instances[$id];
	}

	/**
	 * Method to get the producer table object
	 *
	 * This function uses a static variable to store the table name of the producer table to
	 * it instantiates. You can call this function statically to set the table name if
	 * needed.
	 *
	 * @access 	public
	 * @param	string	The producer table name to be used
	 * @param	string	The producer table prefix to be used
	 * @return	object	The producer table object
	 * @since	1.5
	 */
	function &getTable( $type = null, $prefix = 'msTable' )
	{
		static $tabletype;

		//Set the default tabletype;
		if(!isset($tabletype)) {
			$tabletype['name'] 	= 'Producent';
			$tabletype['prefix']	= 'msTable';
		}

		//Set a custom table type is defined
		if(isset($type)) {
			$tabletype['name'] 	= $type;
			$tabletype['prefix']	= $prefix;
		}

		// Create the producent table object 
		$table 	=& JTable::getInstance( $tabletype['name'], $tabletype['prefix'] );
		return $table;
	}

	/**
	 * Method to bind an associative array of data to a producer object
	 *
	 * @access 	public
	 * @param 	array 	$array 	The associative array to bind to the object
	 * @return 	boolean 	True on success
	 * @since 1.5
	 */
	function bind(& $array)
	{
		// Bind the array
		if (!$this->setProperties($array)) {
			$this->setError("Nie można zbindować tablicy na obiekt producenta.");
			return false;
		}

		// Make sure its an integer
		$this->id = (int) $this->id;

		return true;
	}

	/**
	 * Method to save the msProducent object to the database
	 *
	 * @access 	public
	 * @return 	boolean		True on success
	 * @since 1.0
	 */
	function save( )
	{
		// Utworz obiekt tablicy
		$table =& $this->getTable();
		// Pobiez z ob. usera wlasciwosci do ob. tablicy
		$table->bind($this->getProperties());	
	
		/*
		 * Porownanie wlasciwosci dwoch obiektów
		 */
		$isEqualObjects = 0;   //Domyslnie obiekty sa nierowne
		if ($this->id != null)
		{
			// Porownanie wlasciwosci dwoch obiektow
			$isEqualObjects = $table->compareObjects();
			if ($isEqualObjects === -1) {
				// Blad odczytu tabeli
				return false;
			}
		}
		
		// Zapisujemy pola rejestracji tylko gdy user jest nowy 
		// lub sie zmienily jego dane.
		if ($isEqualObjects === 0)
		{
			// Kto zarejestrowal
			$me =& JFactory::getUser();
			$table->whoRegister = $me->get('name');
			unset($table->registerDate);  // Aby MySql nadał nową, aktualną date
		}
		
		// Sprawdz poprawnosc danych to store
		if (!$table->check()) {
			$this->setError($table->getError());
			return false;
		}
		
		// Nadanie nowego numeru producenta
		if (empty($this->id)) {
			$query = 'SELECT max(nrp) 
		                  FROM #__producenci'
		                  ;
		        $db =& JFactory::getDBO();         
		        $db->setQuery( $query );
		        $new_nrp = (int) $db->loadResult() + 1;
		        
		        $table->nrp = $new_nrp;
		}

		// Zapisz dane z obiektu tablicy do tablicy bazy dabych
		if (!$result = $table->store($isEqualObjects)) {
			$this->setError($table->getError());
		}

		// Set the id for the msProducent object in case we created a new user.
		if (empty($this->id)) {
			$this->id = $table->get( 'id' );
		}

		return $result;
	}

	/**
	 * Metoda usuwa rekord obiektu z BD
	 *
	 * @access 	public
	 * @return 	boolean		True on success
	 * @since 1.0
	 */
	function delete( )
	{
		// Create the producer table object
		$table 	=& $this->getTable();
		// Zbinduj wlasciwosci tego obiektu do obiektu tablicy
		$table->bind($this->getProperties());
		
		// Kto wyrejestrowal 
		$me =& JFactory::getUser();
		$table->whoRegister = $me->get('name');
		unset($table->registerDate);  // Aby MySql nadał nową, aktualną date

		$result = false;
		if (!$result = $table->delete($this->id)) {
			$this->setError($table->getError());
		}
		
		return $result;
	}

	/**
	 * Metoda wypelnia obiekt msProducent producentem o numerze id
	 *
	 * @access 	public
	 * @param 	mixed 	$identifier Id producenta do zaladowania
	 * @return 	boolean 	    True on success
	 * @since 1.5
	 */
	function load($id)
	{
		// Tworzy obiekt tablicy producenta
		if (!$table =& $this->getTable()) {
			// Informacja o bledzie w getTable()
			return false;
		}

		 // Laduje obiekt producenta
		 if (!$table->load($id)) {
			JError::raiseWarning( 'SOME_ERROR_CODE', 'msProducent::load: Nie można załadować producenta o id: '.$id );
			return false;
		}

		// Assuming all is well at this point lets bind the data
		$this->setProperties($table->getProperties());

		return true;
	}
}
